home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr11 / pdox693.zip / TI1148.ASC < prev    next >
Text File  |  1992-11-12  |  11KB  |  463 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.   PRODUCT  :  Paradox                               NUMBER  :  1148
  9.   VERSION  :  All
  10.        OS  :  DOS
  11.      DATE  :  November 12, 1992                        PAGE  :  1/7
  12.  
  13.     TITLE  :  How computers and Paradox use numbers
  14.  
  15.  
  16.  
  17.  
  18.   Intended Audience:
  19.   All Paradox users.  Some sections are aimed at PAL programmers.
  20.  
  21.   Purpose:
  22.   This Tech Info Sheet explains how computers handle numbers,
  23.   describes the Paradox numeric formats, and gives solutions to
  24.   some common problems.
  25.  
  26.  
  27.   When you write a number down, you normally use as many digits as
  28.   needed.  For example, the new car you bought might have cost
  29.   $16,758.35.  If you were talking to friends, though, you would
  30.   probably just say, "That car cost me sixteen-point-seven thou."
  31.   Look at the United States budget, and you would not even try to
  32.   figure it down to the nearest penny, eyeballing it as pretty
  33.   close to two trillion dollars.
  34.  
  35.   Let us suppose that you are only allowed to use 8 digits to
  36.   represent any number.  Pretend for a moment that the actual U.S.
  37.   budget is $2,173,516,948,243.82.  That is a total of 15 digits.
  38.   In order to make our 15 digit number fit into 8 slots, we are
  39.   going to need to do two things: round the number, and convert it
  40.   to scientific notation.
  41.  
  42.   Thus, we end up with 0.217352 x 10^13.  If you strip out the
  43.   extra characters, you can write it down in 8 digits as 21735213,
  44.   where the first six digits are the mantissa, and the last two
  45.   digits are the exponent.
  46.  
  47.   This is very similar to the way computers operate: in order to
  48.   speed up computations, they allocate a fixed space to store
  49.   numbers.  The problem with this approach is that you can lose
  50.   some information about the number, due to the rounding.  Some
  51.   computers do store numbers the way humans do, but they are a lot
  52.   slower; this technique is called Binary Coded Decimal (BCD).
  53.  
  54.   In addition to the rounding issue, computers also have to convert
  55.   between human numbers, which is typically decimal notation, and
  56.   computer numbers, which use binary notation.  For example, the
  57.   following two numbers are equivalent:
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.   PRODUCT  :  Paradox                               NUMBER  :  1148
  75.   VERSION  :  All
  76.        OS  :  DOS
  77.      DATE  :  November 12, 1992                        PAGE  :  2/7
  78.  
  79.     TITLE  :  How computers and Paradox use numbers
  80.  
  81.  
  82.  
  83.  
  84.             180                 10110100
  85.           Decimal                Binary
  86.  
  87.   But, there are problems when you start dealing with fractions.
  88.   Consider the following:
  89.  
  90.   Take the fraction 1/3.  Now convert it to a decimal.  You should
  91.   end up with 0.33333333.... repeating.  If the number is truncated
  92.   at three positions, you get 0.333.  If you do the same process to
  93.   2/3, you start with 0.66666666..., then go to 0.666 -- or should
  94.   it be 0.667?
  95.  
  96.   Let us assume that 2/3 is converted to 0.666.  Then, 1/3 + 2/3
  97.   equals 0.999!
  98.  
  99.   In actual practice, computers usually use enough decimal places
  100.   that you will rarely see a difference in the calculation.  The
  101.   only real problem lies when you subtract the results of two long
  102.   calculations to see if the result is zero.
  103.  
  104.   Paradox uses two standard numeric formats, fixed position integer
  105.   and floating point numbers.  Integers take up two characters, and
  106.   have a range of ±32,767.  Because there is no fractional portion,
  107.   integers have no rounding problem, but the range of numbers may
  108.   not be sufficient.  Integers are equivalent to the S type in
  109.   Paradox.
  110.  
  111.   Floating point numbers use 8 bytes, and can represent numbers
  112.   with a range of 10 x ±308, and have approximately 15 digits of
  113.   precision.  You can use either N or $ for floating point, the
  114.   only difference is the formatting.
  115.  
  116.   NOTE: For financial applications that need to be accurate to the
  117.   nearest penny, the maximum amount that can be used is $100
  118.   billion.
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.   PRODUCT  :  Paradox                               NUMBER  :  1148
  141.   VERSION  :  All
  142.        OS  :  DOS
  143.      DATE  :  November 12, 1992                        PAGE  :  3/7
  144.  
  145.     TITLE  :  How computers and Paradox use numbers
  146.  
  147.  
  148.  
  149.  
  150.   SOLUTIONS TO COMMON PROBLEMS:
  151.  
  152.  
  153.   NOTE: If there is any data from Paradox 3.0 or earlier, you must
  154.   do the following steps, as prescribed in the Paradox 3.5 README
  155.   file.  The binary conversion routines were changed in Paradox
  156.   3.5, so that numbers round differently.  For example, if you
  157.   entered $152.85 in 3.0, then tried to Zoom for it in 3.5, Paradox
  158.   would not find it.  This has only a slight effect on how numbers
  159.   get handled, so calculations should produce almost the same
  160.   result.
  161.  
  162.   If you have two numbers that should be equal (i.e. subtract to
  163.   zero), but Paradox does not bring them up when you link them, the
  164.   simplest solution is to do the following (the example assumes
  165.   that the table is called Test):
  166.  
  167.   Tools | ExportImport | Export | Ascii | Delimited |
  168.        Test | Test
  169.  
  170.   Tools | More | Empty | Test | Ok
  171.  
  172.   Tools | ExportImport | Import | Ascii | AppendDelmited |
  173.        Test | Test
  174.  
  175.   This exports the data to ASCII, empties the table, then reimports
  176.   the data.  This technique will not work if the decimal numbers
  177.   are different; for that, you need to do rounding.
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.   PRODUCT  :  Paradox                               NUMBER  :  1148
  207.   VERSION  :  All
  208.        OS  :  DOS
  209.      DATE  :  November 12, 1992                        PAGE  :  4/7
  210.  
  211.     TITLE  :  How computers and Paradox use numbers
  212.  
  213.  
  214.  
  215.  
  216.   NOTE: All methods from this point forward assume a general
  217.   knowledge of PAL programming.
  218.  
  219.  
  220.   Often times, a calculation involving multiplication or division
  221.   will result in a value that has more digits than needed -- or
  222.   wanted.  This is particularly true in financial calculations,
  223.   where the numbers need to be rounded to the nearest penny every
  224.   step of the way.
  225.  
  226.   One way to handle this is store the numbers as pennies: instead
  227.   of $537.25, you store 53725.  You can then use the INT() or
  228.   ROUND() functions to handle any excess digits.  The primary
  229.   advantage of this method is that there is no rounding error
  230.   during addition and subtraction.
  231.  
  232.   The difficulty comes from handling data entry and display of the
  233.   data.  Typically, a PAL program allows the user to enter numbers
  234.   conventionally (e.g. 93.67), then multiplies by 100 (to 9367).
  235.  
  236.   As a general rule for financial calculations, the ROUND()
  237.   function should be used after every multiplication and division.
  238.   For most other calculations, ROUND() or INT() should be used only
  239.   on the final result.
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.   PRODUCT  :  Paradox                               NUMBER  :  1148
  273.   VERSION  :  All
  274.        OS  :  DOS
  275.      DATE  :  November 12, 1992                        PAGE  :  5/7
  276.  
  277.     TITLE  :  How computers and Paradox use numbers
  278.  
  279.  
  280.  
  281.  
  282.   ROUND()
  283.  
  284.   The ROUND() function in Paradox 4.0 has been updated to use
  285.   "Banker's Rounding" in its algorithm.  Most users should be
  286.   unaffected by this change, described below:
  287.  
  288.   In standard rounding, whenever a number lies exactly on the
  289.   halfway point (e.g. 223.65 rounded to one place), you round up
  290.   (getting 223.7).  In Banker's Rounding, you round to the nearest
  291.   even number (in this case, 223.6).
  292.  
  293.   Here is an example that explains why.  Suppose you have the
  294.   following string of numbers, with their equivalents rounded the
  295.   standard way and with Banker's Rounding; underneath is the total:
  296.  
  297.        Original       Standard       Banker's
  298.  
  299.         15.845          15.85          15.84
  300.         21.395          21.40          21.40
  301.          3.165           3.17           3.16
  302.         13.355          13.36          13.36
  303.         ------          -----          -----
  304.         53.760          53.78          53.76
  305.  
  306.   By adding the numbers up with the standard rounding technique,
  307.   the final answer is two pennies high.
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338.   PRODUCT  :  Paradox                               NUMBER  :  1148
  339.   VERSION  :  All
  340.        OS  :  DOS
  341.      DATE  :  November 12, 1992                        PAGE  :  6/7
  342.  
  343.     TITLE  :  How computers and Paradox use numbers
  344.  
  345.  
  346.  
  347.  
  348.   If Banker's Rounding is not desired, the following procedure will
  349.   work:
  350.  
  351.       PROC NormalRound (N, Digits)
  352.       PRIVATE Shift
  353.           Shift = POW (10, Digits)
  354.           RETURN INT ( ( N * Shift ) + IIF(N<0, -.5, .5) ) / Shift
  355.       ENDPROC
  356.  
  357.   For Paradox 3.5 and earlier, use the following instead:
  358.  
  359.       PROC NormalRound (N, Digits)
  360.       PRIVATE Shift, RoundFactor
  361.           Shift = POW (10, Digits)
  362.           IF N < 0 THEN
  363.               RoundFactor = -.5
  364.           ELSE
  365.               RoundFactor = .5
  366.           ENDIF
  367.           RETURN INT ( ( N * Shift ) + RoundFactor ) / Shift
  368.       ENDPROC
  369.  
  370.  
  371.   To understand how NormalRound() works, here is a walkthrough on
  372.   what happens when you round to two decimal places:
  373.  
  374.       N = 542.6475                ; Starting number
  375.       N = 54264.75                ; Multiply by 100, shift 2 places
  376.       N = 54265.25                ; Add 0.5
  377.       N = 54265.                  ; Take the INT()
  378.       N = 542.65                  ; Divide by 100, shift it back
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.  
  401.  
  402.  
  403.  
  404.   PRODUCT  :  Paradox                               NUMBER  :  1148
  405.   VERSION  :  All
  406.        OS  :  DOS
  407.      DATE  :  November 12, 1992                        PAGE  :  7/7
  408.  
  409.     TITLE  :  How computers and Paradox use numbers
  410.  
  411.  
  412.  
  413.  
  414.   PARADOX 3.5
  415.  
  416.   For version 3.5 only, the ROUND() and INT() functions can return
  417.   incorrect values with negative numbers.  Internally, Paradox is
  418.   storing the number as -0, an impossible number.  The solution is
  419.   to add 0 to the expression:
  420.  
  421.       IF 0 + ROUND(Total,2) = 0 THEN ......
  422.  
  423.  
  424.   MISCELLANEOUS TECHNIQUES
  425.  
  426.   In some rare circumstances, a situation similar to the conversion
  427.   problem between 3.0 and 3.5/4.0 can occur.  Supposing that the
  428.   value was in the field [Total], the following PAL code should
  429.   correct the problem:
  430.  
  431.       [Total] = NUMVAL(STRVAL([Total]))
  432.  
  433.   When a PAL variable is assigned from a $ (currency) field, it --
  434.   and all values derived from it -- gets internally formatted
  435.   identically to a currency field.  This can make it difficult to
  436.   determine what the actual value in the variable is, because it
  437.   gets rounded on the screen.  Assuming that the variable is called
  438.   Total, the following code changes Total to a normal numeric
  439.   format:
  440.  
  441.       Total = NUMVAL ( FORMAT ("W25.17,ES", Total) )
  442.  
  443.   DISCLAIMER: You have the right to use this technical information
  444.   subject to the terms of the No-Nonsense License Statement that
  445.   you received with the Borland product to which this information
  446.   pertains.
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.